<?php
class Member_model extends MY_Model {

    function __construct(){
        parent::__construct();
        $this->tbl_name = 'edm_orders';
    }

    function regist($member){
		if($this->in('member',array('nname'=>$member['nname']))) return -1;
    	if($this->in('member',array('email'=>$member['email']))) return -2;

    	$id = $this->save('member',$member);
		
		if(!empty($member['invitor'])){
		    $this->cm->insert('follow',array('uid'=>$id,'fid'=>$member['invitor']));
		    $this->cm->insert('follow',array('fid'=>$id,'uid'=>$member['invitor']));
		}
		
		return 1;
    }
    
    function login($email,$upass){
    	$cond = array();
    	$cond['email'] = $email;
    	$cond['upass'] = md5($upass);
  		$r = $this->row('member',$cond);
    	if(!empty($r->id)){
    		$this->session->set_userdata('loginer',$r);
    		return true;
    	}
    	
    	return false;
    }
    
    function get_hot(){
    	$query = $this->db->from('member m')
    		->select('m.*,count(t.id) as tcount')
    		->join('topic t','t.poster_id=m.id and t.status=1','left')
    		->group_by('m.id')
    		->order_by('tcount desc')
    		->limit(7,0)
    		->get();
    		
    	$list = $query->result();
    	return $list;
    }
    
    function get_fans_by_page($from,$id){
        $query = $this->db->from($this->tbl->follow)
            ->where(array('fid'=>$id));
        $this->count = $this->db->count_all_results();

        $query = $this->db->from($this->tbl->follow.' f')
            ->select('m.*')
            ->join($this->tbl->member.' m','m.id=f.uid','left')
            ->where(array('fid'=>$id))
            ->limit(PAGESIZE_FOLLOW,$from)
            ->get();
        $list = $query->result();
        
        return $list;
    }
    
    function get_follow_by_page($from,$id){
        
        $query = $this->db->from($this->tbl->follow)
            ->where(array('uid'=>$id));
        $this->count = $this->db->count_all_results();

        $query = $this->db->from($this->tbl->follow.' f')
            ->select('m.*')
            ->join($this->tbl->member.' m','m.id=f.fid','left')
            ->where(array('uid'=>$id))
            ->limit(PAGESIZE_FOLLOW,$from)
            ->get();
        $list = $query->result();
        
        return $list;
    }
    
    function get_friend_by_page($from,$id){
        
        $this->db->from($this->tbl->follow.' f')
            ->select('m.*')
            ->join($this->tbl->follow.' ff','ff.uid=f.fid','left')
            ->join($this->tbl->member.' m','m.id=f.fid','left')
            ->where(array('ff.uid <> '=>'','f.uid'=>$id));
            
        $this->count = $this->db->count_all_results();
        
        $query = $this->db->from($this->tbl->follow.' f')
            ->select('m.*')
            ->join($this->tbl->follow.' ff','ff.uid=f.fid','left')
            ->join($this->tbl->member.' m','m.id=f.fid','left')
            ->where(array('ff.uid <> '=>'','f.uid'=>$id))
            ->limit(PAGESIZE_FOLLOW,$from)
            ->get();
            
        $list = $query->result();
        return $list;
    }
    
    function get_by_page($from,$cond,$id=0){
        
        $map = array();
        if(!empty($cond['nname'])){
            $map['nname'] = $cond['nname'];
        }
        
        $this->count = $this->db->from('member')->like($map)->count_all_results();        
        
    	$query = $this->db->from('member')
    	    ->like($map)
    	    ->where(array('id <> '=>$id))
    	    ->order_by('atime desc')
    		->get();
    		
    	$list = $query->result();
    	$arr_follow_id = $this->get_follow_ids($id);
    	
    	foreach ($list as $k=>$v){
    	    $list[$k]->is_followed = in_array($v->id, $arr_follow_id); 
    	}
    	
    	return $list;
        
    }
    
    function get_follow_ids($id){
        $query = $this->db->from($this->tbl->follow.' f')
            ->select('f.fid')
            ->where(array('uid'=>$id))
            ->get();
        $list = $query->result();
        $arr_id = array();
        if(!empty($list) && is_array($list)){
            foreach ($list as $k=>$v){
                $arr_id[] = $v->fid; 
            }
        }
        
        return $arr_id;
    }
    
    
}